using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BardsTaleMapViewer {
  public partial class Form1 : Form {
    private Game _game;
    //private bool _updatingUI = false;

    public Form1 (Game game) {
      InitializeComponent ();

      _game = game;

      _dungeonUI.Items.AddRange (Dungeon._dungeonsName);
      _dungeonUI.SelectedIndex = 0;
    }

    private void _dungeonUI_SelectedIndexChanged (object sender, EventArgs e) {
      //_updatingUI = true;
      int dungeonIndexPlus1 = _dungeonUI.SelectedIndex + 1;
      _levelUI.Maximum = _game._dungeons [dungeonIndexPlus1 - 1]._levels.Count;
      _levelUI.Value = 1;
      //_updatingUI = false;

      _mapUI.Invalidate ();
    }

    private void _levelUI_ValueChanged (object sender, EventArgs e) {
      _mapUI.Invalidate ();
    }

    private void toolStripNumericUpDown1_ValueChanged (object sender, EventArgs e) {
      _mapUI.Invalidate ();
    }

    public Dungeon SelectedDungeon {
      get {
        int dungeonIndexPlus1 = _dungeonUI.SelectedIndex + 1;
        Dungeon dungeon = _game._dungeons [dungeonIndexPlus1 - 1];
        return (dungeon);
      }
    }

    public DungeonLevel SelectedDungeonLevel {
      get {
        Dungeon dungeon = SelectedDungeon;
        int levelIndexPlus1 = (int) _levelUI.Value;
        return (dungeon._levels [levelIndexPlus1 - 1]);
      }
    }

    private void _doubleBufferedPanel_Paint (object sender, PaintEventArgs e) {
      _mapUI.Height = (DungeonLevel.Height + 2) * ((int) _sizeUI.Value);

      e.Graphics.Clear (Color.Black);
      DungeonLevel dungeonLevel = SelectedDungeonLevel;
      dungeonLevel.Draw (e.Graphics, 0, 0, (int) _sizeUI.Value, _game._beastiary);

      _text1UI.Text = dungeonLevel.BuildTexts ();
      _text2UI.Text = dungeonLevel.BuildMonsters (_game._beastiary);
      _text3UI.Text = dungeonLevel.BuildEvents ();
    }
  }
}
